<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/index.css" />
  </head>
  <body>
    <div class="container">
      <ul class="top">
        <!-- 基础模板 -->
        <!-- 顶级的 标题 -->
        <li>
          <a href="javascript:;">首页</a>
          <!-- 二级标题 -->
          <ul class="sub">
            <li>
              <a href="javascript:;">
                <span>砂锅厨具</span>
                <img
                  src="https://yanxuan.nosdn.127.net/3102b963e7a3c74b9d2ae90e4380da65.png?quality=95&imageView"
                  alt=""
                />
              </a>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <script src="./axios.js"></script>
    <script>
      // 1.打开页面调用
      window.onload = function () {
        axios({
          url: 'http://ajax-api.itheima.net/api/category/top',
          // method:"get",
        }).then((res) => {
          console.log('res:', res)
          // 继续发请求 根据一级分类的个数 发送对应的请求
          const arr = res.data.data.map((v) => {
            return axios({
              url: 'http://ajax-api.itheima.net/api/category/sub',
              method: 'get',
              params: {
                id: v.id,
              },
            })
          })
          // console.log('arr:', arr)
          // Promise.all 等待所有二级分类
          Promise.all(arr).then((twoRes) => {
            console.log('twoRes:', twoRes)
            // 数据数组-map->html数组-join->innerHTML
            const html = twoRes
              .map((twoItem) => {
                return `
              <li>
                <a href="javascript:;">${twoItem.data.data.name}</a>
                <!-- 二级标题 -->
                <ul class="sub">
                  ${twoItem.data.data.children
                    .map((son) => {
                      return `<li>
                        <a href="javascript:;">
                          <span>${son.name}</span>
                          <img src="${son.picture}" alt="" />
                        </a>
                      </li>`
                    })
                    .join('')}
                </ul>
              </li>
              `
              })
              .join('')
            document.querySelector('.top').innerHTML = html
          })
        })
      }
    </script>
  </body>
</html>
